#!/bin/bash
#
# This script reads the APM Secret Token from the Elastic Agent policy and stores it
# in a local file to be used as Terraform output from this module.

KIBANA_ENDPOINT=${kibana_url}/api/fleet/package_policies/elastic-cloud-apm
KIBANA_AUTH=elastic:${elastic_password}

curl -s -u $${KIBANA_AUTH} $${KIBANA_ENDPOINT} $${KIBANA_ENDPOINT} \
  | jq -r '.item | select(.inputs[].policy_template == "apmserver") .inputs[].vars.secret_token.value' \
  | uniq \
  > "${secret_token_file}"
